<template>

  <!--  <el-dialog-->
  <!--    :visible.sync="ShopConfigDialogVisible"-->
  <!--    title="配置门店"-->
  <!--  >-->
  <div class="tenantManagement-container">

    <el-collapse-transition>
      <div v-show="this.moreQueryFlag" class="more-query">
        <!-- 更多查找 -->
        <vab-query-form>
          <vab-query-form-left-panel :span="24">
            <el-form :inline="true" :model="queryForm" @submit.native.prevent>


            </el-form>
          </vab-query-form-left-panel>

        </vab-query-form>
        <el-divider></el-divider>
      </div>
    </el-collapse-transition>

    <!-- 主要操作  -->
    <!--    <vab-query-form>-->
    <!--      <vab-query-form-left-panel :span="10">-->
    <!--        &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--        &lt;!&ndash;          v-if="$perms('shop_shopconfig_insert')"&ndash;&gt;-->
    <!--        &lt;!&ndash;          icon="el-icon-plus"&ndash;&gt;-->
    <!--        &lt;!&ndash;          type="primary"&ndash;&gt;-->
    <!--        &lt;!&ndash;          @click="handleInsert"&ndash;&gt;-->
    <!--        &lt;!&ndash;        > 添加&ndash;&gt;-->
    <!--        &lt;!&ndash;        </el-button>&ndash;&gt;-->

    <!--      </vab-query-form-left-panel>-->
    <!--      <vab-query-form-right-panel :span="14">-->
    <!--        <el-form :inline="true" :model="queryForm" @submit.native.prevent>-->


    <!--          <el-form-item>-->
    <!--            <el-select v-model="form" placeholder="请选择店铺名称" value-key="id">-->
    <!--              <el-option-->
    <!--                v-for="item in list"-->
    <!--                :key="item.id"-->
    <!--                :label="item.orgName"-->
    <!--                :value="item">-->
    <!--              </el-option>-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->

    <!--          <el-form-item>-->
    <!--            <el-button icon="el-icon-search" type="primary" @click="queryData">-->
    <!--              刷新-->
    <!--            </el-button>-->


    <!--          </el-form-item>-->
    <!--        </el-form>-->
    <!--      </vab-query-form-right-panel>-->
    <!--    </vab-query-form>-->


    <el-form ref="form" v-loading="listLoading" :model="form" :rules="rules" label-width="105px">
      <el-row :gutter="10">

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="店铺名称" prop="orgName">
            <el-input v-model="form.orgName" autocomplete="off" placeholder="店铺名称"></el-input>
            <div class="el-form-item__error miaosu">
              店铺名称
            </div>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="经纬度" prop="logLat">
            <el-input v-model="form.logLat" autocomplete="off" placeholder="东经,北纬"></el-input>
            <div class="el-form-item__error miaosu">
              东经,北纬 例如 116.404,39.915 <a href="http://api.map.baidu.com/lbsapi/getpoint/index.html">点击我</a> 获取
            </div>
          </el-form-item>

        </el-col>


        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>


        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
            <div class="el-form-item__error miaosu">
              多个手机号用,隔开,,,,, 英文的逗号 不是，
            </div>
          </el-form-item>

        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="标签" prop="tag">
            <el-input v-model="form.tag" autocomplete="off"></el-input>
            <div class="el-form-item__error miaosu">
              多个标签用,隔开,,,,, 英文的逗号 不是，
            </div>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="描述" prop="describe">
            <el-input v-model="form.remark" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="wifi" prop="wifi">
            <el-input v-model="form.wifi" autocomplete="off"></el-input>

            <div class="el-form-item__error miaosu">
              例如：sdfsdf_5G,ky8888 用,隔开。,,,, 英文的逗号 不是，
            </div>
          </el-form-item>
        </el-col>

        <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <el-form-item label="图片" prop="images">
            <UploadImage ref="UploadImage" :imgageUrl="form.images" @returnImgUrl="returnImgUrl"></UploadImage>
            <div class="el-form-item__error miaosu">
              第一张图为封面图
            </div>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center;margin-top: 20px">
      <el-button type="primary" @click="save">确 定</el-button>
    </div>

    <!--    <import ref="import" @fetchData="fetchData"></import>-->

  </div>
  <!--  </el-dialog>-->

</template>

<script>
import {
  doDelete,
  doInsert,
  doUpdate,
  getList
} from "@/api/system/org/orgManagement";
import {isNull} from "@/utils/validate";
import {formateDate} from "@/utils/format";
import UploadImage from "@/views/modules/common/UploadImage";
import 'zx-quill/dist/zx-quill.css'
import {uploadJsonFile} from "@/api/commom";
import {vueButtonClickBan} from "@/utils";
import {isNotNull} from "@/utils/valiargs";
import VabQuill from 'zx-quill'

export default {

  name: "ShopConfigManagement",
  components: {UploadImage, VabQuill},

  props: {
    QorgIds: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      form: {
        // 设置默认值
        type: 0,
        version: 0
      },
      rules: {
        logLat: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], images: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], address: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], phone: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], tag: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], orgName: [
          {required: true, trigger: "blur", message: "必须填写"},
        ]
      },
      ShopConfigDialogVisible: false,
      list: null,
      listLoading: true,
      
      layout: "total, prev, pager, next, sizes, jumper",
      total: 0,
      selectRows: "",
      elementLoadingText: "正在加载...",
      moreQueryFlag: false,
      queryForm: {
        pageNo: 1,
        pageSize: 10,
        name_LIKE: "",
        value_LIKE: "",
        tenantId_EQ: null,
      },
      dict: {},
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  created() {
    this.show()
  },
  mounted() {
  },
  methods: {

    returnImgUrl(e) {
      // console.log(e)
      this.form.images = e || ""
    },

    showEdit(row) {
      if (!row) {
        this.title = "添加";
      } else {
        this.title = "编辑";
        this.form = Object.assign({}, row);
      }
      this.dialogFormVisible = true;
    },
// 处理 form数据
    handlerFormData(formData) {
      if (!isNull(formData)) {

        if (!isNull(formData.tag)) {
          formData.tag = formData.tag.replace(/，/g, ","); // 替换所有逗号为空字符串
        }
        if (!isNull(formData.phone)) {
          formData.phone = formData.phone.replace(/，/g, ","); // 替换所有逗号为空字符串
        }
        if (!isNull(formData.wifi)) {
          formData.wifi = formData.wifi.replace(/，/g, ","); // 替换所有逗号为空字符串
        }
        if (!isNull(formData.logLat)) {
          formData.logLat = formData.logLat.replace(/，/g, ","); // 替换所有逗号为空字符串
        }
        if (!isNull(formData.images)) {
          formData.images = formData.images.replace(/，/g, ","); // 替换所有逗号为空字符串
        }


        for (let key in formData) {
          // 对于时间类进行处理
          if ("[object Date]" === Object.prototype.toString.call(formData[key])) {
            formData[key] = formateDate(formData[key], 'yyyy-MM-dd hh:mm:ss');
          }
        }
      }
    },
    async save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          // 处理数据
          this.handlerFormData(this.form);

          if (!isNull(this.form.type) && this.form.type === 3) {
            let {data} = await uploadJsonFile("json", this.form.value);
            this.form.value = data.fileStoragePath

          }

          // 修改
          if (!isNull(this.form.id)) {
            const {msg} = await doUpdate(this.form);
            this.$baseMessage(msg, "success");
          } else {
            const {msg} = await doInsert(this.form);
            this.$baseMessage(msg, "success");
          }
          this.queryData()
          // await this.$emit("fetchData");
          // this.close();
        } else {
          return false;
        }
      });
    },
    show(id) {
      this.ShopConfigDialogVisible = true
      // this.queryForm.tenantId_EQ = id;
      this.fetchData();
    },
    setSelectRows(val) {
      this.selectRows = val;
    },
    handleInsert(row) {
      this.$refs["edit"].showEdit();
    },
    handleUpdate(row) {
      if (row.id) {
        this.$refs["edit"].showEdit(row);
      }
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm("你确定要删除当前项吗", null, async () => {
          const {msg} = await doDelete({id: row.id});
          this.$baseMessage(msg, "success");
          await this.fetchData();
        });
      } else {
        if (this.selectRows.length > 0) {
          const ids = this.selectRows.map((item) => item.id).join();
          this.$baseConfirm("你确定要删除选中项吗", null, async () => {
            const {msg} = await doDeleteAll({ids});
            this.$baseMessage(msg, "success");
            await this.fetchData();
          });
        } else {
          this.$baseMessage("未选中任何行", "error");
          return false;
        }
      }
    },
    // 导出excel
    handleExportExcel(el) {
      // 导出按钮防抖处理 默认限制为10秒
      vueButtonClickBan(el, 10);

      // 执行导出
      doExportExcel(this.queryForm);
    },
    // 导入excel
    handleImportExcel() {
      this.$refs["import"].show();
    },


    handleSizeChange(val) {
      this.queryForm.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val;
      this.fetchData();
    },
    moreQuery() {
      this.moreQueryFlag = !this.moreQueryFlag;
    },
    queryData() {


      this.queryForm.pageNo = 1;
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const {data} = await getList(this.queryForm);
      if (isNotNull(data) && data.rows.length > 0) {
        this.list = data.rows;
        this.form = this.list[0]
        this.total = data.total;
      } else {

      }
      setTimeout(() => {
        this.listLoading = false;
      }, 300);
    },
  },
};
</script>
<style scoped>
.miaosu {
  color: #999;
  right: 10px;
  left: auto;
}
</style>
